package com.windfone.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.windfone.common.Constant;
import com.windfone.model.Fitting;
import com.windfone.model.Product;
import com.windfone.service.FittingService;
import com.windfone.service.ProductService;

public class RecommendTag extends TagSupport {
	/** serialVersionUID */
	private static final long serialVersionUID = 1L;
	private String commendType;
	private int commendFlag;

	private ProductService productService;
	private FittingService fittingService;
	private List<Product> recommendProducts;
	private List<Product> newProducts;
	private List<Fitting> fittings;
	private String attriName;
	public String getAttriName() {
		return attriName;
	}

	public void setAttriName(String attriName) {
		this.attriName = attriName;
	}

	public List<Fitting> getFittings() {
		return fittings;
	}

	public void setFittings(List<Fitting> fittings) {
		this.fittings = fittings;
	}

	public ProductService getProductService() {
		return productService;
	}

	public FittingService getFittingService() {
		return fittingService;
	}

	public List<Product> getRecommendProducts() {
		return recommendProducts;
	}

	public List<Product> getNewProducts() {
		return newProducts;
	}

	public void setFittingService(FittingService fittingService) {
		this.fittingService = fittingService;
	}

	public void setRecommendProducts(List<Product> recommendProducts) {
		this.recommendProducts = recommendProducts;
	}

	public void setNewProducts(List<Product> newProducts) {
		this.newProducts = newProducts;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public String getCommendType() {
		return commendType;
	}

	public void setCommendType(String commendType) {
		this.commendType = commendType;
	}

 

	public int getCommendFlag() {
		return commendFlag;
	}

	public void setCommendFlag(int commendFlag) {
		this.commendFlag = commendFlag;
	}

//	@Override
//	public int doEndTag() throws JspException {
	public int doStartTag() throws JspException {
		ServletContext sc = pageContext.getServletContext();
		if ( productService == null )
			productService = (ProductService) WebApplicationContextUtils.getWebApplicationContext(sc).getBean("productService");
		if ( fittingService == null )
			fittingService = (FittingService) WebApplicationContextUtils.getWebApplicationContext(sc).getBean("fittingService");
		
//			recommendService = (RecommendService) WebApplicationContextUtils.getWebApplicationContext(sc).getBean("recommendService");
		if ( Constant.FIRST_PAGE_HOT_MOBILE.equals(commendType)){
			recommendProducts = productService.getCommendProductByCommendType(Constant.FIRST_PAGE_HOT_PRODUCT);
			this.pageContext.setAttribute(attriName,recommendProducts);
		}
		if ( Constant.MOBILE_CENTER_PAGE_HOT_MOBILE.equals(commendType)){
			recommendProducts = productService.getCommendProductByCommendType(Constant.MOBILE_PAGE_HOT_PRODUCT);
			this.pageContext.setAttribute(attriName,recommendProducts);
		}
		if ( Constant.FIRST_PAGE_COMMEND_MOBILE.equals(commendType)){
			recommendProducts = productService.getCommendProductByCommendType(Constant.FIRST_PAGE_COMMEND_PRODUCT);
			this.pageContext.setAttribute(attriName,recommendProducts);
		}
		if ( Constant.FIRST_PAGE_NEW_MOBILE.equals(commendType)){
			newProducts = productService.getCommendProductByCommendType(Constant.FIRST_PAGE_NEW_COMMEND_PRODUCT);
			this.pageContext.setAttribute(attriName,newProducts);
		}
		if ( Constant.FIRST_PAGE_NEW_FITTING.equals(commendType)){
			fittings = fittingService.getCommendFittigByCommendType(Constant.FIRST_PAGE_NEW_COMMEND_FITTING);
			this.pageContext.setAttribute(attriName,fittings);
		}
		if ( Constant.PEIJIAN_PAGE_FITTING.equals(commendType)){
			fittings = fittingService.getCommendFittigByCommendType(Constant.PEIJIAN_PAGE_COMMEND_FITTING);
			this.pageContext.setAttribute(attriName,fittings);
		}
		if ( Constant.PEIJIAN_PAGE_HOT_FITTING.equals(commendType)){
			fittings = fittingService.getCommendFittigByCommendType(Constant.PEIJIAN_PAGE_HOT_COMMEND_FITTING);
			this.pageContext.setAttribute(attriName,fittings);
		}
//		try {
//			pageContext.getOut().print(	"Hello World This is My Tag=" + commendType);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return super.doStartTag();
		return(SKIP_BODY);
	}
}

